package com.seedeer.framework.web.interceptor;

import com.seedeer.common.constant.ParamConsts;
import com.seedeer.project.domain.entity.SysParam;
import com.seedeer.project.service.system.ISysParamService;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wangqiang
 */
public class RevisionInterceptor implements HandlerInterceptor {

    private final ISysParamService sysParamService;

    public RevisionInterceptor(ISysParamService sysParamService) {
        this.sysParamService = sysParamService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        SysParam sysParam = sysParamService.findParam(ParamConsts.VERSION_NUM);
        response.setHeader("revision", sysParam == null ? null : sysParam.getParamValue());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }
}
